# \project_v1\app\controller\request.py
from flask import Blueprint, jsonify,render_template, request
from app.models.base import db
from app.models.request import Request
from app.models.llmAnswer import LLMAnswer

requestBP = Blueprint('request',__name__)

@requestBP.route('/<string:t_name>/<int:t_id>', methods=['GET','POST'])
def submit_request(t_id,t_name):
    if request.method == 'GET':
        return render_template('request.html',id=t_id,name=t_name)
    else:
        data = request.json
        a_id = data.get('A_ID')
        if a_id is not None:
            llm_answer = LLMAnswer.query.get(a_id)
            if not llm_answer:
                return jsonify({'error': 'A_ID not found in LLMAnswer table'}), 400
            with db.auto_commit():
                new_request = Request(
                    
                    RequestID=None,
                    A_ID=data.get('A_ID') or None,
                    CourseCategory=data.get('CourseCategory') or None,
                    CourseName=data.get('CourseName') or None,
                    EID=None,
                    Explanation=data.get('Explanation') or None,
                    ScoreUpdateCount=data.get('ScoreUpdateCount') or None,
                    Request_Type=data.get('Request_Type'),
                    Approve_Type='wait',
                    T_id=t_id
                )
                with db.auto_commit():
                    db.session.add(new_request)
            return jsonify({'message': 'Request submitted successfully'}),200

            